package com.sky.controller.user;

import com.sky.dto.AddressBookDTO;
import com.sky.result.Result;
import com.sky.service.user.AddressBookService;
import com.sky.vo.AddressBookVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api("地址簿")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result addAddr(@RequestBody AddressBookDTO dto){

        addressBookService.save(dto);
        return Result.success();
    }

    /**
     * 查询用户所有地址
     */
    @ApiOperation("查询用户的所有地址")
    @GetMapping("/list")
    public Result queryList(){
        List<AddressBookVO> vos = addressBookService.queryList();
        return Result.success(vos);
    }

    /**
     * 查询默认地址
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result queryDefAddr(){
        AddressBookVO addressBookVO = addressBookService.queryDefAddr();
        return Result.success(addressBookVO);
    }

    /**
     * 修改地址
     */
    @ApiOperation("修改地址")
    @PutMapping
    public Result update(@RequestBody AddressBookDTO dto){
        addressBookService.save(dto);
        return Result.success();
    }

    /**
     * 删除地址
     */
    @ApiOperation("删除地址")
    @DeleteMapping
    public Result delById(Long id){
        addressBookService.delById(id);
        return Result.success();
    }

    /**
     * 根据id查询地址
     */
    @ApiOperation("根据id查询地址")
    @GetMapping ("/{id}")
    public Result queryById(@PathVariable Long id){
        AddressBookVO addressBookVO = addressBookService.queryById(id);
        return Result.success(addressBookVO);
    }

    /**
     * 设置默认地址
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefAddr(@RequestBody AddressBookDTO dto){
        addressBookService.setDefAddr(dto);
        return Result.success();
    }
}
